import re
import hashlib
import os
import json
import traceback
import redis
from InterfaceAutoTest.settings import redis_port
pool=redis.ConnectionPool(host='',port=redis_port,decode_responses=True)
redis_obj=redis.Redis(connection_pool=pool)
def get_unique_number_value(unique_number):
    data=None
    try:
        redis_value=redis_obj.get(unique_number)
        if redis_value:
            data=redis_value
            redis_obj.set(unique_number,int(redis_value)+1)
        else:
            data=1000
            redis_obj.set(unique_number,data)
    except Exception as e:
        data=None
    finally:
        return data
def md5(s):
    m5=hashlib.md5()
    m5.update(s.encode("utf-8"))
    m5_value=m5.hexdigest()
    return m5_value
def data_preprocess(global_key,requestData):
    try:
        if re.search(r"\$\{unique_num\d+\}",requestData):
            var_name=re.search(r"\$\{(unique_num\d+)\}",requestData).group(1)
            var_value=get_unique_number_value(var_name)
            requestData=re.sub(r"\$\{unique_num\d+\}",str(var_value),requestData)
            var_name=var_name.split("_")[1]
            global_var=json.loads(os.environ[global_key])
            global_var[var_name]=var_value
            os.environ[global_key]=json.dumps(global_var)

        if re.search(r"\$\{\w+\(.+\)\}",requestData):
            var_pass=re.search(r"\$\{(\w+\(.+\))\}",requestData).group(1)
            requestData=re.sub(r"\$\{\w+\(.+\)\}",eval(var_pass),requestData)
        if re.search(r"\$\{(\w+)\}",requestData):
            for var_name in re.findall(r"\$\{(\w+)\}",requestData):
                requestData=re.sub(r"\$\{%s\}" % var_name,str(json.loads(os.environ[global_key])[var_name]),requestData)
        return 0,requestData
    except Exception as e:
        return 1,{},traceback.format_exc()
def data_postprocess(global_key,request_data,extract_var):
    var_name=extract_var.split("||")[0]
    reg_exp=extract_var.split("||")[1]
    if re.search(reg_exp,request_data):
        global_vars=json.loads(os.environ[global_key])
        global_vars[var_name]=re.search(reg_exp,request_data).group(1)
        os.environ[global_key]=json.dumps(global_vars)
    return
def assert_result(response_obj,key_word):
    try:
        if '&&' in key_word:
            key_word_list=key_word.split('&&')
            flag=True
            exception_info=''
            for key_word in key_word_list:
                if key_word:
                    if not (key_word in json.dumps(response_obj.json,ensure_ascii=False)):
                        flag=False
                        exception_info="keyword: {} not matched from response, assert failed".format(key_word)
                    else:
                        print("断言关键字【{}】匹配成功".format(key_word))
            if flag:
                print("接口断言成功")
            else:
                print("接口断言失败")
            return flag,exception_info
        else:
            if key_word in json.dumps(response_obj.json(),ensure_ascii=False):
                return True
            else:
                return False
    except Exception as e:
        return  False,traceback.format_exc()
if __name__=='__main__':
    print(get_unique_number_value("unique_num1"))